package cate.game.play.skill.passive.talent;

import cate.common.table.d.GD;
import cate.common.table.d.GDBuff;
import cate.common.util.XT;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.skill.passive.PassiveHandler;

/**
 * 死亡后下一回合有18%的概率复活，每回合增加5%的概率复活，恢复最大生命值30%，5回合内必定复活，一场战斗生效1次
 */
public class 回魂PH extends PassiveHandler {

	private int 初始概率;

	private int 提升概率;

	private double 恢复系数;

	private int 最大回合;

	//初始概率=1800&提升概率=500&恢复系数=3000&最大回合=5
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		初始概率 = args.getInt("初始概率", 0);
		提升概率 = args.getInt("提升概率", 0);
		恢复系数 = args.getWDouble("恢复系数", 0d);
		最大回合 = args.getInt("最大回合", 0);
	}

	private boolean settle;

	private int chance;

	private int addRound;

	@Override
	public void onRoundBegin(ActionCtx action) {
		if (settle) {
			return;
		}
		if (skill.owner.isAlive()) {
			return;
		}
		if (skill.owner.buff.containType(GDBuff.Type.禁止回魂)) {
			return;
		}
		calChance();
		if (!XT.chance(chance)) {
			return;
		}
		settle = skill.owner.attr.resurrect(action, skill.owner.attr.total().hpTotal() * 恢复系数);
	}

	@Override
	public void onResurrect(ActionCtx action) {
		chance = 0;
		addRound = 0;
	}

	private void calChance() {
		if (chance == 0) {
			chance = 初始概率;
		} else {
			if (addRound <= 最大回合 - 1) {
				addRound++;
				chance += 提升概率;
			} else {
				chance = GD.W_10000;
			}
		}
	}
}
